package servlet;

import bean.Fruit;
import dao.impI.FruitDaoImpI;
import service.ImpI.FruitServiceImpI;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/findProductServlet",loadOnStartup = 1,
    initParams = {
        @WebInitParam(name="encoding",value = "utf-8")
    }
)
public class FindProductServlet extends HttpServlet {
    String encoding=null;
    @Override
    public void init(ServletConfig config) throws ServletException {
        encoding = config.getInitParameter("encoding");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding(encoding);
        String id = req.getParameter("id");
        System.out.println("当前的id是:"+id);
        //FruitDaoImpI fruitDaoImpI = new FruitDaoImpI();
        //Fruit fruit=fruitDaoImpI.findProductById(Integer.parseInt(id));
        FruitServiceImpI fruitServiceImpI = new FruitServiceImpI();
        Fruit fruit = fruitServiceImpI.findProductById(Integer.parseInt(id));
        System.out.println("FindProductServlet的fruit："+fruit);
        req.setAttribute("f",fruit);
        req.getRequestDispatcher("/update.jsp").forward(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }




}
